package com.androidx.p013lv.mine.activity;

import android.graphics.BitmapFactory;
import android.os.Build;
import android.text.TextUtils;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import com.androidx.p013lv.base.bean.UserInfo;
import com.androidx.p013lv.base.dialog.ProgressBarDialog;
import com.androidx.p013lv.base.http.BaseRes;
import com.androidx.p013lv.base.interfaces.Key;
import com.androidx.p013lv.base.p015ui.BaseActivity;
import com.androidx.p013lv.base.utils.GlideEngine;
import com.androidx.p013lv.base.utils.LogUtils;
import com.androidx.p013lv.base.utils.PictureStyle;
import com.androidx.p013lv.base.utils.QRHelper;
import com.androidx.p013lv.base.utils.SpUtils;
import com.androidx.p013lv.base.utils.ToastUtils;
import com.androidx.p013lv.mine.C1207R;
import com.androidx.p013lv.mine.databinding.ActivityRetrieveAccCredentialLayoutBinding;
import com.androidx.p013lv.mine.model.RecoverAccountModel;
import com.gyf.immersionbar.ImmersionBar;
import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.config.PictureMimeType;
import com.luck.picture.lib.entity.LocalMedia;
import com.luck.picture.lib.listener.OnResultCallbackListener;
import java.util.List;
import p000a.p002b.RxPermissions;
import p023io.reactivex.functions.Consumer;

/* renamed from: com.androidx.lv.mine.activity.RetrieveAccCredentialActivity */
public class RetrieveAccCredentialActivity extends BaseActivity<ActivityRetrieveAccCredentialLayoutBinding> {
    private ProgressBarDialog loading;
    private RecoverAccountModel model;

    /* access modifiers changed from: protected */
    @Override // com.androidx.p013lv.base.p015ui.BaseActivity
    public void initImmersionBar() {
        super.initImmersionBar();
        ImmersionBar.with(this).titleBar(((ActivityRetrieveAccCredentialLayoutBinding) this.binding).toolbar).init();
    }

    @Override // com.androidx.p013lv.base.p015ui.BaseActivity
    public int setLayout() {
        return C1207R.C1212layout.activity_retrieve_acc_credential_layout;
    }

    /* access modifiers changed from: protected */
    @Override // com.androidx.p013lv.base.p015ui.BaseActivity
    public void initView() {
        super.initView();
        this.model = (RecoverAccountModel) new ViewModelProvider(this).get(RecoverAccountModel.class);
        ((ActivityRetrieveAccCredentialLayoutBinding) this.binding).setClick(new OnClick());
        this.loading = new ProgressBarDialog(this);
        this.model.getRecoverAccData().observe(this, new Observer<BaseRes<UserInfo>>() {
            /* class com.androidx.p013lv.mine.activity.RetrieveAccCredentialActivity.C12641 */

            public void onChanged(BaseRes<UserInfo> baseRes) {
                if (RetrieveAccCredentialActivity.this.loading != null) {
                    RetrieveAccCredentialActivity.this.loading.dismiss();
                }
                if (baseRes.getCode() != 200 || baseRes.getData() == null) {
                    ToastUtils.getInstance().showWrong(baseRes.getMsg());
                    return;
                }
                UserInfo data = baseRes.getData();
                SpUtils.getInstance().setUserInfo(data);
                SpUtils.getInstance().put(Key.TOKEN, data.getToken());
                ToastUtils.getInstance().showCorrect("賬號切換成功");
                RetrieveAccCredentialActivity.this.finish();
            }
        });
    }

    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    private void requestPicturePermission() {
        if (Build.VERSION.SDK_INT >= 23) {
            new RxPermissions(this).request("android.permission.READ_EXTERNAL_STORAGE", "android.permission.CAMERA").subscribe(new Consumer<Boolean>() {
                /* class com.androidx.p013lv.mine.activity.RetrieveAccCredentialActivity.C12652 */

                public void accept(Boolean bool) throws Exception {
                    if (bool.booleanValue()) {
                        RetrieveAccCredentialActivity.this.selectPic();
                    } else {
                        ToastUtils.getInstance().showSigh("未授權權限，請在設置中允許權限");
                    }
                }
            });
        } else {
            selectPic();
        }
    }

    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    private void selectPic() {
        PictureSelector.create(this).openGallery(PictureMimeType.ofImage()).imageEngine(GlideEngine.createGlideEngine()).setPictureStyle(PictureStyle.getInstance(this).getPictureParameterStyle()).setPictureCropStyle(PictureStyle.getInstance(this).getCropParameterStyle()).isCamera(false).selectionMode(1).forResult(new OnResultCallbackListener<LocalMedia>() {
            /* class com.androidx.p013lv.mine.activity.RetrieveAccCredentialActivity.C12663 */

            @Override // com.luck.picture.lib.listener.OnResultCallbackListener
            public void onResult(List<LocalMedia> list) {
                String str;
                if (list != null && list.size() > 0) {
                    try {
                        LocalMedia localMedia = list.get(0);
                        if (TextUtils.isEmpty(localMedia.getAndroidQToPath())) {
                            str = localMedia.getPath();
                        } else {
                            str = localMedia.getAndroidQToPath();
                        }
                        RetrieveAccCredentialActivity.this.identifyTheQRCode(str);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }

            @Override // com.luck.picture.lib.listener.OnResultCallbackListener
            public void onCancel() {
                LogUtils.m63e("RetrieveAccCredential", "PictureSelector Cancel");
            }
        });
    }

    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    private void identifyTheQRCode(String str) {
        this.loading.show();
        String result = QRHelper.getResult(BitmapFactory.decodeFile(str));
        LogUtils.m63e("---->", result);
        if (TextUtils.isEmpty(result)) {
            ProgressBarDialog progressBarDialog = this.loading;
            if (progressBarDialog != null) {
                progressBarDialog.dismiss();
            }
            ToastUtils.getInstance().showSigh("無效的二維碼");
            return;
        }
        String[] split = result.split("\\*");
        if (split.length < 2) {
            ProgressBarDialog progressBarDialog2 = this.loading;
            if (progressBarDialog2 != null) {
                progressBarDialog2.dismiss();
            }
            ToastUtils.getInstance().showSigh("非賬號憑證二維碼");
            return;
        }
        this.model.setRecoverAccData(split[0], split[1]);
    }

    /* access modifiers changed from: protected */
    @Override // androidx.appcompat.app.AppCompatActivity, androidx.fragment.app.FragmentActivity, com.androidx.p013lv.base.p015ui.BaseActivity
    public void onDestroy() {
        RecoverAccountModel recoverAccountModel = this.model;
        if (recoverAccountModel != null) {
            recoverAccountModel.cancelHttp();
        }
        super.onDestroy();
    }

    /* renamed from: com.androidx.lv.mine.activity.RetrieveAccCredentialActivity$OnClick */
    public class OnClick {
        public OnClick() {
        }

        public void btnBack() {
            RetrieveAccCredentialActivity.this.finish();
        }

        public void scanningCredentials() {
            RetrieveAccCredentialActivity.this.requestPicturePermission();
        }
    }
}
